Repository navigation
Darling analysis: the parameter sensitivity fact and drill-down keep each plan's newest row without sorting the whole window (#9492) - #9497
Conversation
| FROM query_stats AS q | ||
| WHERE q.server_id = $1 | ||
| AND q.collection_time = n.newest_time | ||
| AND q.collection_id = n.newest_id |
There was a problem hiding this comment.
Performance (non-blocking, needs a check on a real store): this probe has only server_id, collection_time and collection_id to work with, so the planner uses idx_query_stats_time (server_id, collection_time). The PR body's EXPLAIN shows that index. On that index, each probe reads every query_stats row the server wrote in the winning collection and fetches each one from the heap to test collection_id. On the 500-plan seed that is 500 rows per probe. On a busy server it is thousands of rows per collection, multiplied by every plan that passes the rough filter. On a TimescaleDB store the probe can also land in a compressed chunk.
One option: add AND q.query_hash = n.query_hash when n.query_hash is not null. That lets idx_query_stats_server_hash_time (server_id, query_hash, collection_time DESC) narrow each probe to a few rows. NULL-hash keys would need their own arm, because = drops them. Either way, the per-probe cost belongs in the chunked-store re-time the body defers to the nightly run.
|
Correctness: the drill-down's inline-text read still breaks ties arbitrarily (
Ties are common. Every row of one collection is written with the same This contradicts two statements:
Fix (one line, and it matches Lite's ORDER BY q.database_name, q.query_hash, q.query_plan_hash, q.collection_time DESC, q.collection_id DESCAlso update the doc comment so it no longer refers to Coverage: the new |
There was a problem hiding this comment.
Verdict: CHANGES REQUESTED
Claims
- VERIFIED — Both reads group by
database_name, query_hash, query_plan_hashand takemax(ARRAY[...]), starting with the collection time in epoch µs and thencollection_id—PgFactCollector.QueryPerf.cs:232-246,PgDrillDownCollector.Queries.cs:328-342. - VERIFIED — Every field keeps its old output type — every array member is
bigint(PerformanceMonitor.Collectors/QueryStatsCollector.cs:991-1021), andcreation_timeis rebuilt astimestampatPgFactCollector.QueryPerf.cs:258/PgDrillDownCollector.Queries.cs:356. The store is PG17/18, whereextractreturnsnumeric, so the round trip is exact. - VERIFIED — The leading (time, id) pair is never NULL —
PgSchemaGenerator.cs:75(collection_id bigint NOT NULL), and the window's range predicate oncollection_timeexcludes NULL anyway. Ids are per row (CollectionIdGenerator.Next()per COPY row,DarlingCollectorRunner.StoreWriteAndWatermarks.cs:747). - VERIFIED — NULL key parts form one group, as they did under
PARTITION BY— standardGROUP BYsemantics; the NULL-key case inParameterSensitivityNewestRowLiveTests.SeedRows. - VERIFIED — The WHERE conditions, parameters and their order, the final ORDER BY and the SELECT list are unchanged — the hunks; the reader's ordinals in
PgDrillDownCollector.Queries.cs:555-575still line up with the final SELECT at:403-427. - VERIFIED — The drill-down's
has_inline_textandquery_text_digestcome from aLEFT JOIN LATERALonquery_statsbyserver_id, the winning time and the winning id, withLIMIT 1—PgDrillDownCollector.Queries.cs:383-397. - VERIFIED —
PlanAdvisorySqland the plan regression statement are untouched —gh pr diff --name-onlyand the hunk ranges. - VERIFIED — The new live class seeds the five listed cases and asserts each one against both reads —
ParameterSensitivityNewestRowLiveTests.csSeedRows/AssertNewestRows. - VERIFIED — Two existing guards were adjusted: the
EXTRACT(... FROMskip, and the oracle's text replacement now matchest.has_inline_text—DarlingAnalysisPipelineTests.cs:289,PgFactCollectorTests.cs:307,StatementDrillDownTextFetchLiveTests.cs:296. - UNVERIFIED — The lateral lookup runs only for plans that pass the rough filter — this depends on where the planner places the filter. It needs an EXPLAIN on a real store. The body's
loops=462is consistent with it. - UNVERIFIED — The revert proof (
Expected: 100, Actual: 20on the old SQL) — needs a live Postgres run. - UNVERIFIED — The EXPLAIN figures and the release note's "about three times faster" — needs a live, chunked TimescaleDB store. The body defers this to the nightly run.
- UNVERIFIED — Release build with 0 warnings, the pre-push guard counts and the listed class runs — needs a build and a test run.
- REFUTED — "pick the same row every time when two rows share the newest collection time" (release note) and "The only behaviour change: … the larger
collection_idnow wins" — the drill-down's inline text comes fromParameterSensitiveInlineTextSql, whoseDISTINCT ONorders bycollection_time DESCalone (PgDrillDownCollector.Queries.cs:459). On a tie it can return text from a row other than the one the ranking kept.
Riskiest lines
Darling/PerformanceMonitor.Darling.Analysis/PgDrillDownCollector.Queries.cs:459— the inline-text read has nocollection_idtiebreak, so on a tie its text can come from a different row than the drill-down's figures — UNCOVERED. The new tie case's winner has no inline text, so this read never runs in it.Darling/PerformanceMonitor.Darling.Analysis/PgDrillDownCollector.Queries.cs:394— the lateral lookup by winning time and id. A wrong key would give the winner a different row's text flag and digest — covered byParameterSensitivityNewestRowLiveTests.TheDrillDown_KeepsEachPlansNewestRow_AndReadsItsTextFieldsFromThatRow_AgainstDevPostgres(asserts the winner'shas_inline_textfalse and its digest).Darling/PerformanceMonitor.Darling.Analysis/PgFactCollector.QueryPerf.cs:232— the array's element order decides which row is newest, and the positions read back at:257-264must match it — covered byParameterSensitivityNewestRowLiveTests.TheFact_KeepsEachPlansNewestRow_AndTheLargerIdOnATiedTime_AgainstDevPostgres(asserts tie winner, older-row exclusion, NULL fields, and the creation time to the microsecond).
Traps near this diff
- "
MAX(max_dop)grouped byquery_hashis the highest DOP ANY plan for that statement text ever ran at … An aggregate across plans needs provenance (which plan, when) or the newest plan's reading." — does not apply — themaxis over a whole-row array led by (time, id), so every field comes from one row (the newest), and the group includesquery_plan_hash(PgFactCollector.QueryPerf.cs:232-251). - "Lite and Darling ship in parity (CONTRIBUTING's two-store rule): a fix to one SKU's reader, tool, alert or description without the counterpart's is a finding" — does not apply — Lite already breaks ties on
collection_id DESC(Lite/Analysis/DuckDbFactCollector.QueryPerf.cs:264,Lite/Analysis/DrillDownCollector.Queries.cs:315). This PR brings Darling's two ranking reads in line with it. The remaining gap atPgDrillDownCollector.Queries.cs:459is reported under Divergence. - "Ruling (#5833): tests are load-bearing. … Never add one, and never answer a trap with a test that reads source text" — does not apply — no new source-reading guard is added. The two edits (
DarlingAnalysisPipelineTests.cs:289,PgFactCollectorTests.cs:307) adjust existing scans, and the new class is a behaviour test. - "A test that reads the clock twice measures the CI scheduler" — does not apply —
ParameterSensitivityNewestRowLiveTestsreadsLiveClock.Now()once into the staticWindowEnd, andWindow()derives both ends from it. - "A 'latest snapshot' read anchored on
= (SELECT MAX(collection_time) …)must project the anchor so the payload can say WHEN" — does not apply — these are per-plan newest-row reads feeding a fact and a drill-down, not a snapshot payload anchored on one global time (PgFactCollector.QueryPerf.cs:228-251). - "
.gitattributessaystext=auto eol=crlf… A diff that touches every line of a file is a line-ending conversion until proven otherwise" — does not apply — every hunk is region-scoped (for examplePgDrillDownCollector.Queries.cs@@ -306,61 +306,98 @@).
Divergence (blind read vs PR body)
- The body says that on a tie the drill-down now keeps the same row every time. The ranking read does. The inline-text read that supplies the printed text (
PgDrillDownCollector.Queries.cs:459) still orders bycollection_time DESCalone. Its doc comment (:432-435) still describes the row as "itsrn = 1", andrnno longer exists. Fix: add, q.collection_id DESCto that ORDER BY and add a tie test where both tied rows carry different inline text. Details are in the top-level comment. - The body's EXPLAIN shows each lateral probe served by
idx_query_stats_time (server_id, collection_time). That index makes each probe read every row of the winning collection for the server, and the body doesn't discuss that cost. It is fine on a 500-plan seed, but unmeasured on busy or compressed stores. This is non-blocking and covered in an inline comment on:394.
Reviewer checklist
- On a chunked TimescaleDB store, run
EXPLAIN (ANALYZE, BUFFERS)on both reads over a 7-day window. Confirm there's no per-chunk sort under the aggregate, and check the lateral's per-probe rows and buffers on a busy server. - Run
ParameterSensitivityNewestRowLiveTestsandStatementDrillDownTextFetchLiveTestsagainst a live Postgres (DARLING_TEST_PG) after the tiebreak fix. - Do a release build of
Darling.Testsand confirm there are no new warnings.
There was a problem hiding this comment.
Verdict: CHANGES REQUESTED
Claims
- VERIFIED — Both reads group by
database_name, query_hash, query_plan_hashand takemax(ARRAY[...]), starting with the collection time in epoch µs and thencollection_id—PgFactCollector.QueryPerf.cs:232-246,PgDrillDownCollector.Queries.cs:328-342. - VERIFIED — Every field keeps its old output type — every array member is
bigint(PerformanceMonitor.Collectors/QueryStatsCollector.cs:991-1021), andcreation_timeis rebuilt astimestampatPgFactCollector.QueryPerf.cs:258/PgDrillDownCollector.Queries.cs:356. The store is PG17/18, whereextractreturnsnumeric, so the round trip is exact. - VERIFIED — The leading (time, id) pair is never NULL —
PgSchemaGenerator.cs:75(collection_id bigint NOT NULL), and the window's range predicate oncollection_timeexcludes NULL anyway. Ids are per row (CollectionIdGenerator.Next()per COPY row,DarlingCollectorRunner.StoreWriteAndWatermarks.cs:747). - VERIFIED — NULL key parts form one group, as they did under
PARTITION BY— standardGROUP BYsemantics; the NULL-key case inParameterSensitivityNewestRowLiveTests.SeedRows. - VERIFIED — The WHERE conditions, parameters and their order, the final ORDER BY and the SELECT list are unchanged — the hunks; the reader's ordinals in
PgDrillDownCollector.Queries.cs:555-575still line up with the final SELECT at:403-427. - VERIFIED — The drill-down's
has_inline_textandquery_text_digestcome from aLEFT JOIN LATERALonquery_statsbyserver_id, the winning time and the winning id, withLIMIT 1—PgDrillDownCollector.Queries.cs:383-397. - VERIFIED —
PlanAdvisorySqland the plan regression statement are untouched —gh pr diff --name-onlyand the hunk ranges. - VERIFIED — The new live class seeds the five listed cases and asserts each one against both reads —
ParameterSensitivityNewestRowLiveTests.csSeedRows/AssertNewestRows. - VERIFIED — Two existing guards were adjusted: the
EXTRACT(... FROMskip, and the oracle's text replacement now matchest.has_inline_text—DarlingAnalysisPipelineTests.cs:289,PgFactCollectorTests.cs:307,StatementDrillDownTextFetchLiveTests.cs:296. - UNVERIFIED — The lateral lookup runs only for plans that pass the rough filter — this depends on where the planner places the filter. It needs an EXPLAIN on a real store. The body's
loops=462is consistent with it. - UNVERIFIED — The revert proof (
Expected: 100, Actual: 20on the old SQL) — needs a live Postgres run. - UNVERIFIED — The EXPLAIN figures and the release note's "about three times faster" — needs a live, chunked TimescaleDB store. The body defers this to the nightly run.
- UNVERIFIED — Release build with 0 warnings, the pre-push guard counts and the listed class runs — needs a build and a test run.
- REFUTED — "pick the same row every time when two rows share the newest collection time" (release note) and "The only behaviour change: … the larger
collection_idnow wins" — the drill-down's inline text comes fromParameterSensitiveInlineTextSql, whoseDISTINCT ONorders bycollection_time DESCalone (PgDrillDownCollector.Queries.cs:459). On a tie it can return text from a row other than the one the ranking kept.
Riskiest lines
Darling/PerformanceMonitor.Darling.Analysis/PgDrillDownCollector.Queries.cs:459— the inline-text read has nocollection_idtiebreak, so on a tie its text can come from a different row than the drill-down's figures — UNCOVERED. The new tie case's winner has no inline text, so this read never runs in it.Darling/PerformanceMonitor.Darling.Analysis/PgDrillDownCollector.Queries.cs:394— the lateral lookup by winning time and id. A wrong key would give the winner a different row's text flag and digest — covered byParameterSensitivityNewestRowLiveTests.TheDrillDown_KeepsEachPlansNewestRow_AndReadsItsTextFieldsFromThatRow_AgainstDevPostgres(asserts the winner'shas_inline_textfalse and its digest).Darling/PerformanceMonitor.Darling.Analysis/PgFactCollector.QueryPerf.cs:232— the array's element order decides which row is newest, and the positions read back at:257-264must match it — covered byParameterSensitivityNewestRowLiveTests.TheFact_KeepsEachPlansNewestRow_AndTheLargerIdOnATiedTime_AgainstDevPostgres(asserts tie winner, older-row exclusion, NULL fields, and the creation time to the microsecond).
Traps near this diff
- "
MAX(max_dop)grouped byquery_hashis the highest DOP ANY plan for that statement text ever ran at … An aggregate across plans needs provenance (which plan, when) or the newest plan's reading." — does not apply — themaxis over a whole-row array led by (time, id), so every field comes from one row (the newest), and the group includesquery_plan_hash(PgFactCollector.QueryPerf.cs:232-251). - "Lite and Darling ship in parity (CONTRIBUTING's two-store rule): a fix to one SKU's reader, tool, alert or description without the counterpart's is a finding" — does not apply — Lite already breaks ties on
collection_id DESC(Lite/Analysis/DuckDbFactCollector.QueryPerf.cs:264,Lite/Analysis/DrillDownCollector.Queries.cs:315). This PR brings Darling's two ranking reads in line with it. The remaining gap atPgDrillDownCollector.Queries.cs:459is reported under Divergence. - "Ruling (#5833): tests are load-bearing. … Never add one, and never answer a trap with a test that reads source text" — does not apply — no new source-reading guard is added. The two edits (
DarlingAnalysisPipelineTests.cs:289,PgFactCollectorTests.cs:307) adjust existing scans, and the new class is a behaviour test. - "A test that reads the clock twice measures the CI scheduler" — does not apply —
ParameterSensitivityNewestRowLiveTestsreadsLiveClock.Now()once into the staticWindowEnd, andWindow()derives both ends from it. - "A 'latest snapshot' read anchored on
= (SELECT MAX(collection_time) …)must project the anchor so the payload can say WHEN" — does not apply — these are per-plan newest-row reads feeding a fact and a drill-down, not a snapshot payload anchored on one global time (PgFactCollector.QueryPerf.cs:228-251). - "
.gitattributessaystext=auto eol=crlf… A diff that touches every line of a file is a line-ending conversion until proven otherwise" — does not apply — every hunk is region-scoped (for examplePgDrillDownCollector.Queries.cs@@ -306,61 +306,98 @@).
Divergence (blind read vs PR body)
- The body says that on a tie the drill-down now keeps the same row every time. The ranking read does. The inline-text read that supplies the printed text (
PgDrillDownCollector.Queries.cs:459) still orders bycollection_time DESCalone. Its doc comment (:432-435) still describes the row as "itsrn = 1", andrnno longer exists. Fix: add, q.collection_id DESCto that ORDER BY and add a tie test where both tied rows carry different inline text. Details are in the top-level comment. - The body's EXPLAIN shows each lateral probe served by
idx_query_stats_time (server_id, collection_time). That index makes each probe read every row of the winning collection for the server, and the body doesn't discuss that cost. It is fine on a 500-plan seed, but unmeasured on busy or compressed stores. This is non-blocking and covered in an inline comment on:394.
Reviewer checklist
- On a chunked TimescaleDB store, run
EXPLAIN (ANALYZE, BUFFERS)on both reads over a 7-day window. Confirm there's no per-chunk sort under the aggregate, and check the lateral's per-probe rows and buffers on a busy server. - Run
ParameterSensitivityNewestRowLiveTestsandStatementDrillDownTextFetchLiveTestsagainst a live Postgres (DARLING_TEST_PG) after the tiebreak fix. - Do a release build of
Darling.Testsand confirm there are no new warnings.
There was a problem hiding this comment.
Verdict: LGTM
Claims
- VERIFIED — The inline-text read now breaks a tie on the newest time by collection_id DESC, the same row the ranking keeps — PgDrillDownCollector.Queries.cs:459 adds
q.collection_id DESCto theDISTINCT ONORDER BY. - VERIFIED — A new live test seeds two rows at the same newest time, both with inline text, and asserts the larger id's text is returned — ParameterSensitivityNewestRowLiveTests.cs
TheDrillDownInlineText_OnATiedTime_ComesFromTheLargerCollectionId_AgainstDevPostgres. The larger id (-94_920_140) is inserted last, so insertion order does not decide it. Its ratio of 2 keeps it out of the ranking reads. - VERIFIED — The test refactor (
WithSeededStoreAsync) leaves the existing reads unchanged — the ordinal mapping moved into the callback unchanged. - VERIFIED — The covering-index test now seeds from
t.Start.AddHours(2), so the rows stay in the window when the hour turns — QueryFleetSummaryCoveringIndexLiveTests.cs:153. This is test-only.
Riskiest lines
- PgDrillDownCollector.Queries.cs:459 — the tie-break in the
DISTINCT ONordering — covered byTheDrillDownInlineText_OnATiedTime_ComesFromTheLargerCollectionId_AgainstDevPostgres(it needsDARLING_TEST_PGand skips without it). - ParameterSensitivityNewestRowLiveTests.cs:~203 — the
WithSeededStoreAsyncextraction, which carries the shared seeding and cleanup — covered by the existing newest-row tests that call it. - QueryFleetSummaryCoveringIndexLiveTests.cs:153 — the seed window shifted by 2h — UNCOVERED, and test-only.
Traps near this diff
- No trap checked in detail for this light-tier follow-up. The change is a SQL ORDER BY tie-break plus tests, and it adds no new query shape. — does not apply — PgDrillDownCollector.Queries.cs:459.
Divergence (blind read vs PR body)
None found in this follow-up range. I did not run gh pr view for this pass.
Reviewer checklist
- Run the new live test against a dev Postgres with
DARLING_TEST_PGset. A read-only tree cannot run it.
Superseded by claude[bot]'s LGTM on 5bd5c9e: #9497 (review)
Claude posting for Erik Darling.
What changed
The parameter sensitivity fact (
PgFactCollector.ParameterSensitivitySql) and its drill-down (PgDrillDownCollector.ParameterSensitiveSql) found each plan's newest row by numbering every row of the window withROW_NUMBER() ... ORDER BY collection_time DESC. That sorts every chunk's rows, holds each sort until the merge ends, and picks an arbitrary row when two rows of one key share the newest time.Both reads now group by the same key the
PARTITION BYused (database_name, query_hash, query_plan_hash) and takemax(ARRAY[...])of an array that starts with the row's collection time (epoch microseconds) andcollection_id, then the fields. Arrays compare element by element, so the largest array is the newest row, and on a tied time the row with the largercollection_id. Each field is read back by position, and each keeps its old output type. A NULL field after the (time, id) pair never decides the winner and comes back NULL. NULL key parts group together, as they did underPARTITION BY. Every WHERE condition, the parameters and their order, the final ORDER BY and the reply are unchanged. The only behaviour change: on two rows of one key at the newest time, the largercollection_idnow wins (before, it was arbitrary).max(ARRAY[...])overv_query_stats, then anewest_rowsstep that reads the fields back, then the same filter, SELECT and ORDER BY as before.query_stats, thenhas_inline_textandquery_text_digestcome fromLEFT JOIN LATERALonquery_statsbyserver_id, the winner'scollection_timeandcollection_id,LIMIT 1. The lookup runs only for plans that pass the rough filter.PlanAdvisorySqland the plan regression statement are untouched.Field type check
collect.query_statscolumns, from the migration ladder:collection_idisbigint NOT NULLandcollection_timeistimestamp NOT NULL, so the leading pair is never NULL.collection_timeandcreation_time(bothtimestampwithout time zone, so epoch microseconds round trip exactly),collection_id,execution_count,min_worker_time,max_worker_time,min_grant_kb,max_grant_kb,min_spills,max_spills(allbigint, nullable).has_inline_text(a boolean) andquery_text_digest(bytea), drill-down only.Tests
New live class
ParameterSensitivityNewestRowLiveTests, run against both reads over the same seeded rows. The seeded plans:has_inline_text(false, though the losing row has inline text) and the digest.Two existing guards needed a small change, not a looser rule. The scans that check every
FROMtarget resolves to a view, table or CTE now skip theFROMinsideEXTRACT(epoch FROM col), which names a column. The drill-down text comparison builds its old-shape query by text replacement, and that replacement now matches the lateral lookup's column.Revert proof: with both reads put back to the old SQL, both new tests fail (
Expected: 100, Actual: 20): the old read keeps the smaller-id row of the tied pair. With the new SQL both pass.EXPLAIN
The test store has no TimescaleDB, so there are no chunks and the per-chunk sort under a merge append cannot appear or be shown absent here. Seeded 240,000 rows (500 plans, 480 snapshots, 5 days) and ran
EXPLAIN (ANALYZE, BUFFERS)over a 7 day window. Top nodes:No sort sits under the aggregate. The chunked plan (aggregate over a merge append with no per-chunk sort) needs a TimescaleDB store and is the re-time the issue's "Done when" asks for after the next nightly.
Runs
Darling.Tests, 0 errors, 0 warnings.pre-push.sh: GuardDarling.Tests5004 passed, 0 failed. GuardLite.Tests1248 passed, 0 failed.GATE STOP: the selection was the whole suite (darling 3239, lite 533), and no selected class ran.ParameterSensitivityNewestRowLiveTests,*ParameterSensitiv*,PgFactCollectorTests,PlanRegressionReadShapeTests,ServerLocalRowConversionTests,StatementDrillDownTextFetchLiveTests,FactsNameQueries*,FactThresholdsTests,CreationTimeClockFrameDisciplineTests,AnalysisPlanAdvisoryGzReadTests,DarlingAnalysisPipelineTests,DrillDownSqlTests,DrillDownDopProvenance*,QueryHighDopStaleMaxDopParityTests,*PlanRegression*(188, 4 skipped),ConsumedTimestampFrameDisciplineTests,DarlingReadsUseOnlyStoreViewsTests,PgTargetFactCollectorTests,PgTargetAnomalyTests,LatestValueLookback*,BadActorFactDrillDownAgreement*.Considered, not added: a tiebreak on the drill-down's final
ORDER BY(unchanged on purpose, it is outside this change).Left as a draft until a field check of the 7 day timing on a chunked store.
Fixes #9492
Release note: the parameter sensitivity checks read a week of history about three times faster, and pick the same row every time when two rows share the newest collection time.
Round 1 fixes
ParameterSensitiveInlineTextSql) now orders bycollection_time DESC, collection_id DESC. On two rows of one plan at the newest time it returns the text of the largercollection_id, the same row the ranking keeps. Its doc comment no longer mentions the old row numbering.TheDrillDownInlineText_OnATiedTime_ComesFromTheLargerCollectionId_AgainstDevPostgres: two rows of one plan share the newest time, both carry inline text, the texts differ, and the smaller id is inserted first. The read must return the larger id's text. The seeded plan has a worker ratio of 2, so the ranking does not report it and the other tests in the class are unchanged. The class's seed and read plumbing now share one helper.*ParameterSensitivityNewestRow*,*StatementDrillDownTextFetch*,*PgDrillDown*,*ParameterSensitiv*(23 tests, 0 failed). Release build, 0 errors.